#include<stdio.h>
#include"getch.h"
#include<stdlib.h>

int main(int argc,const char* argv[])
{
	//迷宫数组 路=0 墙壁=1 角色=2 箱子=3 目标点=4  
	char map[8][8] = {
		{0,0,1,1,1,1,0,0},
		{0,0,1,4,4,1,0,0},
		{0,1,1,0,4,1,1,0},
		{0,1,0,0,3,4,1,0},
		{1,1,0,3,0,0,1,1},
		{1,0,0,1,3,3,0,1},
		{1,0,0,2,0,0,0,1},
		{1,1,1,1,1,1,1,1}
	};
	int x = 6, y = 3; // 角色当前坐标（根据地图初始化）
	
	//游戏开始	
	while(1)
	{
		int cnt = 0;  // 统计完成的箱子数
		
		// 清屏
		system("clear");
		
		//显示地图
		for(int i=0;i<8;i++)
		{
			for(int j=0;j<8;j++)
			{
				switch(map[i][j])
				{
					case 0:printf("  ");break;
					case 1:printf("# ");break;
					case 2:printf("@ ");break;
					case 3:printf("$ ");break;
					case 4:printf("O ");break;
					case 6:printf("@ ");break;
					case 7:printf("$ ");cnt++;break;
				}
			}
			printf("\n");
		}

		//判断游戏结束
		if(cnt == 4)
		{
			printf("游戏结束\n");
			return 0;
		}

		//键盘输入
		int p_x = 0,p_y = 0;
		switch(getch())
		{
			case 183:p_x = -1;break;
			case 184:p_x = 1;break;
			case 185:p_y = 1;break;
			case 186:p_y = -1;break;
		}
		if(p_x != p_y)
		{
			if(0 == map[x+p_x][y+p_y] || 4 == map[x+p_x][y+p_y])		
			{
				map[x+p_x][y+p_y] += 2;
				map[x][y] -= 2;
				x += p_x;
				y += p_y;
			}
			else if((3 == map[x+p_x][y+p_y] || 7 == map[x+p_x][y+p_y]) && (0 == map[x+2*p_x][y+2*p_y] || 4 == map[x+2*p_x][y+2*p_y]))
			{
				map[x+2*p_x][y+2*p_y] += 3;
				map[x+p_x][y+p_y] -= 1;
				map[x][y] -= 2;
				x += p_x;
				y += p_y;
			}
		}
	}
}
